{% extends 'IcsocUIBundle::page.html.twig' %}
{% from 'IcsocUIBundle:Macro:grid.html.twig' import gridWidth,gridNav,gridConfig %}
{% from 'IcsocUIBundle:Macro:actions.html.twig' import timebutton,searchtime,searchForm,searchFormBtn %}

{% block stylesheet %}
    {{ parent() }}
    {% stylesheets
    '@datepicker_css'
    '@jqgird_css'
    '@jquery_ui_css'
    '@gritter_css'
    '@chosen_css'
    filter='cssrewrite' filter='uglifycss'
    %}
    <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}

{% block content %}
    <form action="javascript:search()" method="post" id="searchForm">
        <div class="widget-container-col">
            <div class="widget-box">
                <div class="widget-body">
                    <div class="widget-main no-padding">
                        <table class="table table-bordered table-hover no-padding">
                            <tbody>
                            {{ timebutton() }}
                            {{ searchForm(options) }}
                            {{ searchtime(date.today.start, date.today.end) }}
                            {{ searchFormBtn() }}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <table id="grid-table"></table>
    <div id="grid-pager"></div>
    <div id="dialogconfirm" class="hide row">
        <div class="alert alert-block alert-success">
            <i class="ace-icon fa fa-check green"></i>
            由于数据量过大，下载任务转到<br>后台生成，点击进入生成界面查看进度。
            <i class="ace-icon fa fa-hand-o-right icon-animated-hand-pointer blue"></i>
            <a class="pink" href="#" onclick="showTab('下载任务管理', '{{ path('icsoc_security_develop_download_work_index') }}')" data-toggle="modal">查看进度</a>
        </div>
    </div>
{% endblock %}
{% block footer %}
    {% javascripts
    '@datepicker_js'
    '@jqgird_js'
    '@jquery_ui_js'
    '@gritter_js'
    '@common_js'
    '@chosen_js'
    filter='uglifyjs'
    %}
    <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
    <script type="text/javascript">
        var isSpecial = {{ isSpecial }};
        $(function () {
            $('#start_time').datetimepicker({locale:'zh-cn',format:'YYYY-MM-DD HH:mm:ss',widgetPositioning:{horizontal:'left'}});
            $('#end_time').datetimepicker({locale:'zh-cn',format:'YYYY-MM-DD HH:mm:ss',widgetPositioning:{horizontal:'left'}});
            $('.chosen-select').chosen({width: $(".col-sm-6").width()+'px', allow_single_deselect:true});
            if (isSpecial == 1) {
                $('.forBaidu').hide();
            }
        });

        jQuery(function($) {
            {{ gridWidth() }}
            jQuery(grid_selector).jqGrid({
                caption: "{{ 'Agent State detail Report'|trans }}",
                url: "{{ path('icsoc_report_agentstate_list') }}",
                mtype: 'POST',
                datatype: "json",
                hidegrid:false,
                records:true,
                postData: {param:jsonToString(getCondition())},
                height: '100%',
                colModel:[
                    {name:'ag_num' , index:'ag_num' , label: '{{ 'Agent Num'|trans }}' , width:120 , sortable:true , frozen:true , fixed:true},
                    {name:'ag_name' , index:'ag_name' , label: '{{ 'Agent Name'|trans }}' , width:120 , sortable:true , frozen:true , fixed:true},
                    {% if isEnableGroup %}
                    {name:'group_id' , index:'group_id' , label: '{{ 'Business Group'|trans }}' , width:90 , sortable:true ,fixed:true},
                    {% endif %}
                    {name:'ag_sta_type' , index:'ag_sta_type' , label: '{{ 'Operation'|trans }}' , width:120 , sortable:true , fixed:true},
                    {name:'ag_sta_reason' , index:'ag_sta_reason' , label: '{{ 'Operation Reason'|trans }}' , width:120 , sortable:true , fixed:true},
                    {name:'ag_login_ip' , index:'ag_login_ip' , label: '{{ 'Login IP'|trans }}' ,width:150, sortable:true , fixed:true},
                    {name:'start_time' , index:'start_time' , label: '{{ 'Start Time'|trans }}' ,width:150, sortable:true , fixed:true},
                    {name:'end_time' , index:'end_time' , label: '{{ 'End time'|trans }}' ,width:150, sortable:false , fixed:true},
                    {name:'duration' , index:'duration' , label: '{{ 'Duration'|trans }}' , width:80 , sortable:true , fixed:true},
                    {name:'bend' , index:'bend' , label: '{{ 'Bend'|trans }}' , width:80 , sortable:true , fixed:true}
                ],
                {{ gridConfig() }}
                height: '326',
                rowNum:10,
                rowList: [10,20,30,50,100],
                rownumWidth: 40,
                viewrecords: true,
                sortname:"id",
                sortorder:'desc',
                multiselect: false,
                rownumbers: true
            });
            {{ gridNav(false,false,false,false,true,false) }}
            jQuery(grid_selector).navButtonAdd(
                    pager_selector,
                    {
                        caption:'{{ 'Export Csv'|trans }}',
                        buttonicon:'ace-icon fa fa-file-excel-o  primary',
                        onClickButton:function() {
                            var data = jsonToString(getCondition());
                            getSumMath(data,'csv');
                        }
                    }
            ).navButtonAdd(
                    pager_selector,
                    {
                        caption:'{{ 'Export Excel'|trans }}',
                        buttonicon:'ace-icon fa fa-file-excel-o  primary',
                        onClickButton:function() {
                            var data = jsonToString(getCondition());
                            getSumMath(data,'excel');
                        }
                    }
            );
        });

        /**
         * 重置
         */
        function resetBtn()
        {
            $('#searchForm')[0].reset();
            $(".chosen-select").trigger("chosen:updated");
            search();
        }

        /**
         * 搜索
         */
        function search()
        {
            $('#grid-table').setGridParam({page:1,postData:{param:jsonToString(getCondition())}}).trigger("reloadGrid");
        }

        /**
         * 获取搜索条件
         */
        function getCondition()
        {
            var $start_time = $('#start_time');
            var $end_time = $('#end_time');

            if (!$start_time.val()) {
                $start_time.parent().parent().addClass('has-error');
                $start_time.focus();
                return;
            }

            if (!$end_time.val()) {
                $end_time.parent().parent().addClass('has-error');
                $end_time.focus();
                return;
            }

            var param = {};

            param.ag_num = $.trim($('#ag_num').val());
            param.ag_sta_type = $('#ag_sta_type').val();
            {% if isEnableGroup %}
            param.group_id = $('#group_id').val();
            {% endif %}
            param.start_time = $.trim($('#start_time').val());
            param.end_time = $.trim($('#end_time').val());

            return param;
        }

        /**
         * 设置时间
         *
         * @param type
         */
        function fnSetTime(type)
        {
            var $start_time = $('#start_time');
            var $end_time = $('#end_time');

            switch (type) {
                case 1:
                    $start_time.val('{{ date.yestoday.start }}');
                    $end_time.val('{{ date.yestoday.end }}');
                    break;
                case 2:
                    $start_time.val('{{ date.today.start }}');
                    $end_time.val('{{ date.today.end }}');
                    break;
                case 3:
                    $start_time.val('{{ date.lastweek.start }}');
                    $end_time.val('{{ date.lastweek.end }}');
                    break;
                case 4:
                    $start_time.val('{{ date.thisweek.start }}');
                    $end_time.val('{{ date.thisweek.end }}');
                    break;
                case 5:
                    $start_time.val('{{ date.lastmonth.start }}');
                    $end_time.val('{{ date.lastmonth.end }}');
                    break;
                case 6:
                    $start_time.val('{{ date.thismonth.start }}');
                    $end_time.val('{{ date.thismonth.end }}');
                    break;
                case 7:
                    $start_time.val('{{ date.thisyear.start }}');
                    $end_time.val('{{ date.thisyear.end }}');
                    break;
                case 8:
                    $start_time.val('{{ date.lastyear.start }}');
                    $end_time.val('{{ date.lastyear.end }}');
                    break;
            }

            search();
        }

        function addDownloadWorlk(type,data){
            $.ajax({
                url: "{{ path('icsoc_report_agentstate_list') }}",
                dataType: 'json',
                data: {'export': type,'param':data,'big':true},
                type: 'post',
                success: function (e) {
                    if(e['code'] == 200) {
                        $( "#dialogconfirm" ).removeClass('hide').dialog({
                            resizable: false,
                            modal: false,
                            title_html: true,
                            title:"提示",
                        });
                    } else {
                        gritterAlert('{{ 'Prompt'|trans }}', e['message'],'error');
                    }
                }
            })
        }
        //计算数量 判断用哪一种导出方式
        function getSumMath(data,type){
            $.ajax({
                url: "{{ path('icsoc_report_agentstate_list') }}",
                dataType: 'json',
                data: {'param':data, 'sum':true},
                type: 'post',
                success: function (e) {
                    if(e['code'] == 200) {
                        if(e['count'] < {{ max_export_num }}) {
                            location.href = "{{ path('icsoc_report_agentstate_list')}}?export="+type+"&param="+jsonToString(getCondition());
                        } else {
                            addDownloadWorlk(type,data);
                        }
                    } else {
                        gritterAlert('{{ 'Prompt'|trans }}', e['message'],'error');
                    }
                }
            })
        }

        function showTab(name, url)
        {
            window.parent.createTab(name, url);
            $('#dialogconfirm').dialog('close');
        }
    </script>
{% endblock %}